package cn.bearspark.api.service.main.impl;

import cn.bearspark.api.service.main.VoteService;
import cn.bearspark.api.vo.main.user.UserSlimVO;
import cn.bearspark.api.vo.main.vote.VoteDetailVO;
import cn.bearspark.api.vo.main.vote.VoteReqVO;
import cn.bearspark.common.constant.GlobalConstant;
import cn.bearspark.common.util.ConvertBeanUtil;
import cn.bearspark.framework.web.starter.context.BSRequestContext;
import cn.bearspark.framework.web.starter.vo.ResponseVO;
import cn.bearspark.happening.dto.vote.VoteDetailDTO;
import cn.bearspark.happening.dto.vote.VoteReqDTO;
import cn.bearspark.happening.rpc.VoteRpc;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author f
 */
@Service
public class VoteServiceImpl implements VoteService {

    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private VoteRpc voteRpc;

    @Override
    public ResponseVO<Object> vote(VoteReqVO voteReqVO) {
        long userId = BSRequestContext.getUserId();

        VoteReqDTO voteReqDTO = ConvertBeanUtil.convert(voteReqVO, VoteReqDTO.class);
        voteReqDTO.setVoterId(userId);
        voteRpc.vote(voteReqDTO);

        return ResponseVO.success();
    }

    @Override
    public ResponseVO<VoteDetailVO> detail(Long voteId) {
        long userId = BSRequestContext.getUserId();

        VoteDetailDTO voteDetailDTO = voteRpc.detail(voteId, userId);
        VoteDetailVO voteDetailVO = ConvertBeanUtil.convert(voteDetailDTO, VoteDetailVO.class);

        UserSlimVO publisherInfo = new UserSlimVO();
        publisherInfo.setUserId(voteDetailDTO.getUserId());
        publisherInfo.setUsername(voteDetailDTO.getUsername());
        publisherInfo.setAvatarUrl(voteDetailDTO.getAvatarUrl());
        voteDetailVO.setPublisherInfo(publisherInfo);

        return ResponseVO.success(voteDetailVO);
    }
}
